<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen"
	href="/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen"
	href="/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen"
	href="/layui/css/layui.css">
</head>
<body>
	
	<div class="main-content-inner">
		<!-- 左边服务列表 -->
		<div class="col-sm-4">
			<div class="table-header">
				服务列表&nbsp;&nbsp; <a class="green" href="#"> <i
					class="ace-icon fa fa-plus-circle orange bigger-130 dept-add"></i>
				</a>
			</div>
			<div>
			 
				<div class="widget-body no-padding">
					<table id="dt-service-table"
						class="table table-striped table-bordered table-hover"
						style="width: 100%">
						<thead>
							<tr>
							</tr>
							<tr>
								<th>服务名称</th>
								<th>实例</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
			 
			</div>
		</div>
		<!-- 右边用户列表 -->
		<div class="col-sm-8">
			<div class="col-xs-12">
				<div class="table-header">
					实例列表&nbsp;&nbsp; <a class="green" href="#"> <i
						class="ace-icon fa fa-plus-circle orange bigger-130 user-add"></i>
					</a>
				</div>
				<div>
					<div id="dynamic-table_wrapper"
						class="dataTables_wrapper form-inline no-footer">
						  <div class="widget-body no-padding">
								<table id="dt-instance-table"
									class="table table-striped table-bordered table-hover"
									style="width: 100%">
									<thead>
										<tr>
										</tr>
										<tr>
											<th>主机名称</th>
											<th>IP地址</th>
											<th>端口</th>
											<th>状态</th>
											<th>操作</th>
										</tr>
									</thead>
									<tbody>
									</tbody>
								</table>
							</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	 
</body>
</html>


<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/js/jq.js"></script>
<script type="text/javascript"
	src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript"
	src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" src="/js/my/permission.js"></script>
<script type="text/javascript">
var pers = checkPermission();
var service  ;
var instance  ;
function init(){
	service = 
    	$('#dt-service-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	 "bFilter": false,    //去掉搜索框方法三：这种方法可以
             "bLengthChange": false,   //去掉每页显示多少条数据方法
             "bInfo": false,
             "bPaginate": false,
        	"language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
            
            
            "ajax": function (data, callback, settings) {  
	            	 //封装请求参数  
	            	 var param = {};  
	            	 param.limit =data.length;//页面显示记录条数，在页面显示每页显示多少项的时候  
	            	 param.start = data.start;//开始的记录序号  
	            	 param.page = (data.start / data.length)+1;//当前页码  
	            	 //ajax请求数据  
	            	 $.ajax({  
		            	  type: "GET",  
		            	  url: "/eureka/apps",  
		            	  cache: false, //禁用缓存  
		            	  data: param, //传入组装的参数  
		            	  dataType: "json",  
	            	  "success": function (result) {  
		            	  //封装返回数据  
		            	  var returnData = {};  
		            	   returnData.data = result.applications.application;//返回的数据列表  
		            	   
		            	  //console.log(returnData);  
		            	  //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染  
		            	  //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕  
		            	  callback(returnData);  
		            	  
	            	  }  ,
	            	  "error":function(xhr, textStatus, errorThrown){
	      				var msg = xhr.responseText;
	      				console.log(msg);
	      				var response = JSON.parse(msg);
	      				var code = response.code;
	      				var message = response.message;
	      				if (code == 400) {
	      					layer.msg(message);
	      				} else if (code == 401) {
	      					localStorage.removeItem("token");
	      					layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function(){
	      						location.href = '/login.html';
	                          });
	      				} else if (code == 403) {
	      					console.log("未授权:" + message);
	      					layer.msg('未授权');
	      				} else if (code == 500) {
	      					layer.msg('系统错误：' + message);
	      				}
	      			}
            		 });  
            	 },  
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
                { "data": "name", "defaultContent": ""} , 
                { "data": "instance.length", "defaultContent": ""}  ,
                { 
                	"data": "", 
                	"defaultContent": "",
                	"orderable":false,
                	"render": function (data, type, row) {
                		var isSysJob = row['isSysJob'];
                		if(!isSysJob) {
                			var id = row['name'];
                    		var href = "/eureka/apps?name=" + id;
                            //var edit = buttonInfo(href, "job:add", pers); 
                            var edit = buttonInfo(id, "job:add", pers);
                            return edit    ;
                		} else {
                			return "<span style='color:red'>系统任务无操作</span>";
                		}
                  	}
                }
            ],
        } );
}
 
function info(id){
	
	
	 $("#dt-instance-table").DataTable().clear();
	 $("#dt-instance-table").DataTable().destroy();
	 
	instance = 
    	$('#dt-instance-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	 "bFilter": false,    //去掉搜索框方法三：这种方法可以
             "bLengthChange": false,   //去掉每页显示多少条数据方法
             "bInfo": false,
             "bPaginate": false,
        	"language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
            
            
            "ajax": function (data, callback, settings) {  
	            	 //封装请求参数  
	            	 var param = {};  
	            	 param.limit =data.length;//页面显示记录条数，在页面显示每页显示多少项的时候  
	            	 param.start = data.start;//开始的记录序号  
	            	 param.page = (data.start / data.length)+1;//当前页码  
	            	 //ajax请求数据  
	            	 $.ajax({  
		            	  type: "POST",  
		            	  url: '/eureka/appsmd?name='+id,
		            	  cache: false, //禁用缓存  
		            	  data: param, //传入组装的参数  
		            	  dataType: "json",  
	            	  "success": function (result) {  
		            	  //封装返回数据  
		            	  var returnData = {};  
		            	   returnData.data = result.application.instance;//返回的数据列表  
		            	   
		            	  //console.log(returnData);  
		            	  //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染  
		            	  //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕  
		            	  callback(returnData);  
		            	  
	            	  }  ,
	            	  "error":function(xhr, textStatus, errorThrown){
	      				var msg = xhr.responseText;
	      				console.log(msg);
	      				var response = JSON.parse(msg);
	      				var code = response.code;
	      				var message = response.message;
	      				if (code == 400) {
	      					layer.msg(message);
	      				} else if (code == 401) {
	      					localStorage.removeItem("token");
	      					layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function(){
	      						location.href = '/login.html';
	                          });
	      				} else if (code == 403) {
	      					console.log("未授权:" + message);
	      					layer.msg('未授权');
	      				} else if (code == 500) {
	      					layer.msg('系统错误：' + message);
	      				}
	      			}
            		 });  
            	 },  
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
                { "data": "hostName", "defaultContent": ""} , 
                { "data": "ipAddr", "defaultContent": ""}  ,
                { "data": "port.$", "defaultContent": ""}  ,
                { "data": "status", "defaultContent": ""}  ,
                { 
                	"data": "", 
                	"defaultContent": "",
                	"orderable":false,
                	"render": function (data, type, row) {
                		var isSysJob = row['ipAddr'];
                		if(isSysJob) {
                            //var edit = buttonInfo(href, "job:add", pers); 
                            
                            var ipAddr = row['ipAddr'];
                            var port = row['port'].$;
                            var status =row['status'];
                            var req ={} ;
                            req.ip = ipAddr;
                            req.port = port;
                            req.status = status;
                            var ops = buttonOps(req, "job:add", pers);
                            var refresh = buttonRefresh(req, "job:add", pers);
                            return ops  + refresh  ;
                		} else {
                			return "<span style='color:red'>系统任务无操作</span>";
                		}
                  	}
                }
            ],
        } );
	
     
}

layui.use('layer', function(){
    var layer = layui.layer;
});

function del(id){
    layer.confirm('确定要取消吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            type : 'delete',
            url : '/jobs/'+id,
            success : function(data) {
            	service.ajax.reload();
                layer.msg("取消成功");
            }
        });
        
        layer.close(1);
    });
}



function ops(ip,port,ops){
	
	var req = {} ;
	req.ip = ip ;
	req.port = port ;
	req.operate = ops ;
	var str ;
	if(ops==1){
		str="暂停";
	}else if(ops==3){
		str="恢复";
	}else if(ops==4){
		str="刷新";
	}
	
    layer.confirm('确定要'+str+'吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            type : 'post',
            url : '/eureka/operate',
            data: JSON.stringify(req),
            dataType:"html",
            contentType: "application/json",
            success : function(data) {
            	service.ajax.reload();
                layer.msg("操作成功");
            }
        });
        layer.close(1);
    });
}
 

init();
</script>